package datatypes;

import homeMadeExceptions.Bd_AddrException;

public class BluetoothDeviceAddress {

	private final static BluetoothDeviceAddress broadCastAdresse = new BluetoothDeviceAddress (1,2,3,4,5,6);
	private int firstPart = -1;
	private int secondPart = -1;
	private int thirdPart = -1;
	private int forthPart = -1;
	private int fifthPart = -1;
	private int sixthPart = -1;
	private String bd_Addr = firstPart + ":" + secondPart + ":" + thirdPart + ":" + forthPart + ":" + fifthPart + ":" + sixthPart;

	private BluetoothDeviceAddress (int first, int second, int third, int forth, int fifth, int sixth){
		this.firstPart = first;
		this.secondPart = second;
		this.thirdPart = third;
		this.forthPart = forth;
		this.fifthPart = fifth;
		this.sixthPart = sixth;
		bd_Addr = firstPart + ":" + secondPart + ":" + thirdPart + ":" + forthPart + ":" + fifthPart + ":" + sixthPart;
	}
	
	public BluetoothDeviceAddress(String bd_Addr) throws Bd_AddrException {
		String [] bd_AddrParts = bd_Addr.split(":");
		if (bd_AddrParts.length != 6){
		   throw new Bd_AddrException ("Addressformat Failure");
		}
		try {
			firstPart = Integer.parseInt(bd_AddrParts[0]);
			secondPart = Integer.parseInt(bd_AddrParts[1]);
			thirdPart = Integer.parseInt(bd_AddrParts[2]);
			forthPart = Integer.parseInt(bd_AddrParts[3]);
			fifthPart = Integer.parseInt(bd_AddrParts[4]);
			sixthPart = Integer.parseInt(bd_AddrParts[5]);
			this.bd_Addr = firstPart + ":" + secondPart + ":" + thirdPart + ":" + forthPart + ":" + fifthPart + ":" + sixthPart;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			Bd_AddrException ex = new Bd_AddrException("Address Parsing Failure");
			ex.setStackTrace(e.getStackTrace());
			throw ex;
		}
	}
	
	public String getAddress (){
		return this.bd_Addr;
	}
	
	
	public String getFirstPart(){
		return this.fifthPart + "";
	}
	
	public boolean isEqual (BluetoothDeviceAddress bd_Addr){
		if (bd_Addr != null){
			return this.bd_Addr.equals(bd_Addr.getAddress());		
		}
		return true;
	}
	
	public static BluetoothDeviceAddress getBroadcastAddresse(){
		return broadCastAdresse;
	}
	
}
